package xyz.jpcr.covid19app;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.Toast;

import xyz.jpcr.covid19app.Activity.PerCityData;
import xyz.jpcr.covid19app.databinding.FragmentCollectionBinding;

import static xyz.jpcr.covid19app.Adapter.CityAdapter.CITY_CONFIRMED;
import static xyz.jpcr.covid19app.Adapter.CityAdapter.CITY_CURED;
import static xyz.jpcr.covid19app.Adapter.CityAdapter.CITY_CURRENT_CONFIRMED;
import static xyz.jpcr.covid19app.Adapter.CityAdapter.CITY_DEAD;
import static xyz.jpcr.covid19app.Adapter.CityAdapter.CITY_LAST_UPDATE_TIME;
import static xyz.jpcr.covid19app.Adapter.CityAdapter.CITY_NAME;
import static xyz.jpcr.covid19app.Adapter.CityAdapter.CITY_SUSPECTED;

public class CovidAppWidget extends AppWidgetProvider {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i(context.toString(), "onReceive: " + intent.getAction());
        if(AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(intent.getAction())) {
            Bundle extras = intent.getExtras();
            if (extras != null) {
                int[] appWidgetIds = extras.getIntArray(AppWidgetManager.EXTRA_APPWIDGET_IDS);
                if (appWidgetIds != null && appWidgetIds.length > 0) {
                    Log.i(context.toString(), "onUpdate: now");
                    this.onUpdate(context, AppWidgetManager.getInstance(context), appWidgetIds);
                }
            }
        }
        super.onReceive(context, intent);
    }

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        AppWidgetManager manager = AppWidgetManager.getInstance(context);
        for (int appWidgetId : appWidgetIds) {
            RemoteViews rv = updateWidgetListView(context, appWidgetId);
            manager.notifyAppWidgetViewDataChanged(appWidgetId, R.id.widget_listview);
            manager.updateAppWidget(appWidgetId, rv);
        }
    }

    @Override
    public void onEnabled(Context context) {
    }

    @Override
    public void onDisabled(Context context) {
    }


    private RemoteViews updateWidgetListView(Context context, int appWidgetId) {
        Intent intent = new Intent(context, ListWidgetService.class);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
                appWidgetId);
        intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
        RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.covid_app_widget);
        rv.setRemoteAdapter(appWidgetId, R.id.widget_listview, intent);
        Intent startIntent = new Intent(context, PerCityData.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, startIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        rv.setPendingIntentTemplate(R.id.widget_listview, pendingIntent);
        return rv;



    }
}